package com.tigercel.xiangchengyun.ui.fragments

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.ui.activities.*
import com.tigercel.xiangchengyun.utils.CleanMessageUtil
import com.tigercel.xiangchengyun.utils.PreferencesUtil
import kotlinx.android.synthetic.main.fragment_user.*
import org.jetbrains.anko.support.v4.toast
import java.io.File

class UserFragment : Fragment(), View.OnClickListener {
    private var cacheData: String = ""
    private var localDatas: PreferencesUtil? = null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_user, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ll_about_us.setOnClickListener(this)
        ll_userinfo.setOnClickListener(this)
        ll_invitation_code.setOnClickListener(View.OnClickListener {
            val intent = Intent()
            intent.setClass(activity, WebViewActivity::class.java)
            intent.putExtra("url", "")
            intent.putExtra("title", "邀请码")
            intent.putExtra("id", "")
            startActivity(intent)
        })
        ll_my_comments.setOnClickListener(View.OnClickListener {
            val intent = Intent()
            intent.setClass(activity, MyCommentActivity::class.java)
            startActivity(intent)
        })
        ll_collection.setOnClickListener(View.OnClickListener {
            var intent = Intent()
            intent.setClass(activity, MyCollectionsActivity::class.java)
            startActivity(intent)
        })
        localDatas = PreferencesUtil(activity)
        ll_feedback.setOnClickListener(View.OnClickListener {
            val intent = Intent()
            intent.setClass(activity, FeedbackActivity::class.java)
            startActivity(intent)
        })
        btn_logout.setOnClickListener {
            val intent = Intent()
            intent.setClass(activity, LogoutActivity::class.java)
            startActivity(intent)
        }

    }

    override fun onClick(v: View?) {
        when (v) {
            ll_about_us -> startActivity(Intent(activity, AboutUsActivity::class.java))
            ll_userinfo -> if (localDatas!!.preferences["isAlreadyLogin"] == "false") {
                startActivity(Intent(activity, LoginActivity::class.java))
            } else {
                startActivity(Intent(activity, AccountSettingActivity::class.java))
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (localDatas!!.preferences["name"]!!.isEmpty()) {
            tv_dispaly.text = "点击登录"
        } else {
            tv_dispaly.text = localDatas!!.preferences["name"]
        }
        if (localDatas!!.preferences["isAlreadyLogin"] == "false") {
            btn_logout.visibility = View.GONE
        } else {
            btn_logout.visibility = View.VISIBLE
        }
        if (localDatas!!.preferences["userAvatar"] != "") {
            Glide.with(activity!!).load(File(localDatas!!.preferences["userAvatar"])).into(iv_portrait)
        } else {
            Glide.with(activity!!).load(R.drawable.img_user).into(iv_portrait)
        }
        cacheData = CleanMessageUtil.getTotalCacheSize(context)
        tv_cache.text = cacheData
        li_cleanCache.setOnClickListener(View.OnClickListener {
            CleanMessageUtil.clearAllCache(context)
            cacheData = CleanMessageUtil.getTotalCacheSize(context)
            tv_cache.text = cacheData
            toast("缓存清除成功")
        })
    }
}
